<?php include_once( "_db.php" ); //Database ?>
<?php include_once( "funcdef.php" ); //Functions Definition ?>
<?php include_once( "header.php" ); //Header ?>
<?php include_once( "footer.php" ); //Footer ?>
<?php
//include page header
header_template("Category", $_SERVER['PHP_SELF'], "icon-list-alt");

//db OBJECT invoked!
$db = new DATABASE();
?>
      <div class="row">
        <div class="span6">
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
  //Submit button for Create
  $button_submit_name  = "create-category";
  $button_submit_value = "Create";
  //Hidden category id
  $hidden_category_id = "";
  //Category for Create
  $resCat = array();
  if (isset($_GET["action"]) && isset($_GET["cat_id"])) {
    if ($_GET["action"] == "edit") {
      //Category for Edit
      //construct SELECT query
      $conds = array();
      $conds[] = array(0); //$param_col;
      $conds[] = array(0); //$param_op;
      $conds[] = array($_GET["cat_id"]); //$param_key;
      $query = $db->db_select("category", NULL, $conds);
      //execute SELECT query
      $result = $db->database->query($query);
      //result SELECT query
      $resCat = $result->fetchAll(SQLITE_ASSOC);
      //Submit button for Edit
      $button_submit_name  = "edit-category";
      $button_submit_value = "Edit";
      //Hidden category id
      $hidden_category_id = "<input type='hidden' name='category_id' value='".$resCat[0]['category_id']."' />";
    }
  }
//Category Form
?>
          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="application/x-www-form-urlencoded" class="well form-inline">
            <?php echo $hidden_category_id."\n"; ?>
            <label class="control-label" for="category">Category</label>
            <input type="text" class="input-xlarge" id="category" name="category" value="<?php echo $resCat[0]['category_name']; ?>" />
            <input type="submit" name="<?php echo $button_submit_name; ?>" value="<?php echo $button_submit_value; ?>" class="btn btn-info" />
          </form>
<?php
} elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
  //construct SELECT query
  $query = $db->db_select("category", NULL, NULL);
  //execute SELECT query
  $result = $db->database->query($query);
  //result SELECT query
  $resCat = $result->fetchAll(SQLITE_ASSOC);
  //Create Category
  if ($_POST['create-category'] == "Create") {
    //Form field check NG
    if ($_POST["category"] == "") {
      printAlert("error", "Oh snap!", "Category field is empty.", $_SERVER['PHP_SELF']);
    } else {//Form field check OK
      //Existence check NG
      if (in_array_2d($_POST["category"], $resCat)) {
        printAlert("error", "Oh snap!", "Category already exist.", $_SERVER['PHP_SELF']);
      } else {//Existence check OK
        //construct INSERT query
        $params = array();
        $params[] = array(1); //$param_col;
        $params[] = array($_POST["category"]); //$param_key;
        $query = $db->db_insert("category", $params);
        //execute INSERT query
        $result = $db->database->query($query);
        //Add category NG
        if ($result == FALSE) {
          printAlert("error", "Oh snap!", "Cannot create Category '".$_POST["category"]."'.", $_SERVER['PHP_SELF']);
        } else {//Add category OK
          //INSERT SUCCEED!!!
          printAlert("success", "Well done!", "Category '".$_POST["category"]."' successfully created.", $_SERVER['PHP_SELF']);
        }
      }
    }
  }
  //Edit Category
  if ($_POST['edit-category'] == "Edit") {
    //Form field check NG
    if ($_POST["category"] == "") {
      printAlert("error", "Oh snap!", "Category field is empty.", $_SERVER['PHP_SELF']);
    } else {//Form field check OK
      //ID check NG
      if (!in_array_2d($_POST["category_id"], $resCat)) {
        printAlert("error", "Oh snap!", "Category ID not exist.", $_SERVER['PHP_SELF']);
      } else {//ID check OK
        //Existence check NG
        if (in_array_2d($_POST["category"], $resCat)) {
          printAlert("error", "Oh snap!", "Category already exist.", $_SERVER['PHP_SELF']);
        } else {//Existence check OK
          //construct UPDATE query
          $params = array();
          $params[] = array(1); //$param_col;
          $params[] = array($_POST["category"]); //$param_key;
          $conds = array();
          $conds[] = array(0); //$param_col;
          $conds[] = array(0); //$param_op;
          $conds[] = array($_POST["category_id"]); //$param_key;
          $query = $db->db_update("category", $params, $conds);
          //execute UPDATE query
          $result = $db->database->query($query);
          //Edit category NG
          if ($result == FALSE) {
            printAlert("error", "Oh snap!", "Cannot edit Category '".$_POST["category"]."'.", $_SERVER['PHP_SELF']);
          } else {//Edit category OK
            //UPDATE SUCCEED!!!
            printAlert("success", "Well done!", "Category '".$_POST["category"]."' successfully edited.", $_SERVER['PHP_SELF']);
          }
        }
      }
    }
  }
} else {
  $error_msg = "This script only works with GET and POST requests.";
  printAlert("error", "Oh snap!", $error_msg, $_SERVER['PHP_SELF']);
}

//Delete Category
if (isset($_GET["action"]) && isset($_GET["cat_id"])) {
  //construct SELECT query
  $query = $db->db_select("category", NULL, NULL);
  //execute SELECT query
  $result = $db->database->query($query);
  //result SELECT query
  $resCat = $result->fetchAll(SQLITE_ASSOC);
  if ($_GET["action"] == "delete") {
    //ID check NG
    if (!in_array_2d($_GET["cat_id"], $resCat)) {
      printAlert("error", "Oh snap!", "Category ID not exist.", $_SERVER['PHP_SELF']);
    } else {//ID check OK
      $conds = array();
      $conds[] = array(0); //$param_col;
      $conds[] = array(0); //$param_op;
      $conds[] = array($_GET["cat_id"]); //$param_key;
      $query = $db->db_select("category", NULL, $conds);
      //execute SELECT query
      $result = $db->database->query($query);
      //result SELECT query
      $resCat = $result->fetchAll(SQLITE_ASSOC);
      //construct DELETE query
      $query = $db->db_delete("category", $conds);
      //execute DELETE query
      $result = $db->database->query($query);
      //delete category NG
      if ($result == FALSE) {
        printAlert("error", "Oh snap!", "Cannot delete ".$resCat[0]['category_name'], $_SERVER['PHP_SELF']);
      } else {//delete category OK
        //DELETE SUCCEED!!!
        printAlert("success", "Well done!", $resCat[0]['category_name']." successfully deleted", $_SERVER['PHP_SELF']);
        //CHECK FOR category2product
        //construct DELETE params
        $conds = array();
        $conds[] = array(1); //$param_col;
        $conds[] = array(0); //$param_op;
        $conds[] = array($_GET["cat_id"]); //$param_key;
        //construct DELETE query
        $query = $db->db_delete("category2product", $conds);
        //execute DELETE query
        $result = $db->database->query($query);
        //delete category link NG
        if ($result == FALSE) {
          printAlert("error", "Oh snap!", "Cannot delete ".$resCat[0]['category_name']." link", $_SERVER['PHP_SELF']);
        } else {//delete category link OK
          //DELETE SUCCEED!!!
          printAlert("success", "Well done!", $resCat[0]['category_name']." link successfully deleted", $_SERVER['PHP_SELF']);
        }
      }
    }
  }
}

//construct SELECT query
$query = $db->db_select("category", NULL, NULL);
//execute SELECT query
$result = $db->database->query($query);
//result SELECT query
$resCat = $result->fetchAll(SQLITE_ASSOC);
//List category(s)
?>
          <span class="label label-inverse">Edit / Delete Category</span>
          <div class="well well-small">
            <p>
<?php
//category(s) existence OK
if (!empty($resCat)) {
  //loop trough all category(s)
  foreach ($resCat as $cat) {
    $category_edit_href = $_SERVER['PHP_SELF']."?action=edit&cat_id=".$cat['category_id'];
    $category_delete_href = $_SERVER['PHP_SELF']."?action=delete&cat_id=".$cat['category_id'];
?>
              <span class="label label-default"><?php echo $cat['category_name']; ?></span><a class="label label-info" href="<?php echo $category_edit_href; ?>">&nbsp;&#45;&nbsp;</a><a class="label label-important" href="<?php echo $category_delete_href; ?>">&nbsp;&#215;&nbsp;</a>&nbsp;
<?php
  }
}
?>
            </p>
          </div>
<!--
          <span class="label label-success">Edit Category</span>
          <div class="well well-small">
            <div class="btn-group">
<?php
/*
//category(s) existence OK
if (!empty($resCat)) {
  //loop trough all category(s)
  foreach ($resCat as $cat) {
    $category_edit_href = $_SERVER['PHP_SELF']."?action=edit&cat_id=".$cat['category_id'];
?>
              <a href="<?php echo $category_edit_href; ?>" class="btn btn-mini btn-success"><?php echo $cat['category_name']; ?></a>
<?php
  }
}
?>
            </div>
          </div>
          <span class="label label-important">Delete Category</span>
          <div class="well well-small">
            <div class="btn-group">
<?php
//category(s) existence OK
if (!empty($resCat)) {
  //loop trough all category(s)
  foreach ($resCat as $cat) {
    $category_delete_href = $_SERVER['PHP_SELF']."?action=delete&cat_id=".$cat['category_id'];
?>
              <a href="<?php echo $category_delete_href; ?>" class="btn btn-mini btn-danger"><?php echo $cat['category_name']; ?></a>
<?php
  }
}
*/
?>
            </div>
          </div>
-->
        </div>
      </div>
<?php
//include page footer
footer_template();
?>
